import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home'
import Login from '../views/login/Login.vue'
import Welcome from '../views/home/aside/Welcome'
import User from '../views/home/aside/User'
import Role from '../views/home/aside/Role'
import Menu from '../views/home/aside/Menu'
import { Message } from "element-ui";

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: "/login" },
  { path: '/login', component: Login },

  {
    path: '/home',
    component: Home,
    redirect: "/home/welcome",
    meta:{requireAuth:true},
    children: [
      { path: 'welcome', component: Welcome },
      { path: 'user', component: User },
      { path: 'role', component: Role },
      { path: 'menu', component: Menu }
    ]
  },
]

const router = new VueRouter({
  routes,
  mode: "history"
})
router.beforeEach((to, from, next) => {
  next()
  if (to.path === '/login') {
    next()
  } else {
    if (window.sessionStorage.getItem("Name")) {
      next()
    } else {
      Message({ message: "你还未登录", type: "error" });
      next('/login')
    }
  }
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
